# /usr/bin/python
# -*- coding: utf-8 -*-

import os,sys
BASE_DIR = os.path.dirname(os.path.abspath(os.curdir))
sys.path.append(BASE_DIR)
from models.MySQL import MySQL
from conf.setting import templates 
from util.log import get_stream_logger
from models.Zabbix import ZBX

logger = get_stream_logger("add_template")

Z = ZBX()
templates = Z.get_template(host="")
with MySQL() as cur:
    for template in templates:
        check_cmd = "SELECT * FROM tpl WHERE tpl_name='%s'"%template['name']
        check_res = cur.cur.execute(check_cmd)
        if check_res == 0:
            insert_cmd = "INSERT INTO tpl VALUES (null,'%s','39',1,'root',null)"%template['name']
            res = cur.update(insert_cmd)
            if res != 1:
                logger.info("Template %s insert failed"%template['name'])